package com.xdjd.task.config;

import java.io.IOException;
import java.util.Arrays;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import net.oschina.durcframework.easymybatis.EasymybatisConfig;
import net.oschina.durcframework.easymybatis.ext.SqlSessionFactoryBeanExt;
import net.oschina.durcframework.easymybatis.support.DateFillInsert;
import net.oschina.durcframework.easymybatis.support.DateFillUpdate;

@Configuration
@MapperScan(basePackages = {"com.xdjd.task.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryProduct")
public class DatabaseConfig {

	// mybatis的config文件
//	private static final String mybatisConfigLocation = "classpath:mybatis/mybatisConfig.xml";
	// mybatis的mapper文件
	private static final String mybatisMapperLocations = "classpath:mybatis/*/*.xml";
	// 存放mapper包路径，多个用;隔开
	private static final String basePackage = "com.xdjd.task.mapper";

	@Bean(name = "dbProduct")
	@Primary
	@ConfigurationProperties(prefix = "spring.datasource") // application.properteis中对应属性的前缀
	public DataSource dataSourceMater() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean
	public EasymybatisConfig easymybatisConfig() {
		EasymybatisConfig config = new EasymybatisConfig();
        /* 驼峰转下划线形式，默认是true
         开启后java字段映射成数据库字段将自动转成下划线形式
         如：userAge -> user_age
         如果数据库设计完全遵循下划线形式，可以启用
         这样可以省略Entity中的注解，@Table，@Column都可以不用，只留
        @Id
        @GeneratedValue
        参见：UserInfo.java
        */
		config.setCamel2underline(true);
		config.setFills(Arrays.asList(
				new DateFillInsert("create_time"),
				new DateFillUpdate("modify_time")
		));

		return config;
	}

	@Bean
	public SqlSessionFactory sqlSessionFactoryProduct(@Autowired
													 @Qualifier("dbProduct")DataSource dataSource,EasymybatisConfig config) throws Exception {
		SqlSessionFactoryBeanExt bean = new SqlSessionFactoryBeanExt();

		bean.setDataSource(dataSource);
//		bean.setConfigLocation(this.getResource(mybatisConfigLocation));
		bean.setMapperLocations(this.getResources(mybatisMapperLocations));

		// ====以下是附加属性====

		// dao所在的包名,跟MapperScannerConfigurer的basePackage一致,多个用;隔开
		bean.setBasePackage(basePackage);
		bean.setConfig(config);

		return bean.getObject();

	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplateProduct(@Autowired
													   @Qualifier("sqlSessionFactoryProduct")SqlSessionFactory sessionFactory) throws Exception {
		SqlSessionTemplate template = new SqlSessionTemplate(sessionFactory); // 使用上面配置的Factory
		return template;
	}

	private Resource[] getResources(String path) throws IOException {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		return resolver.getResources(path);
	}

	private Resource getResource(String path) {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		return resolver.getResource(path);
	}
}
